package tgc.edu.exam.custom.dict.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.data.domain.Pageable;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.exam.custom.AjaxResult;
import tgc.edu.exam.custom.DataGridParam;
import tgc.edu.exam.custom.DataGridUtils;
import tgc.edu.exam.custom.dict.entity.SystemData;
import tgc.edu.exam.custom.dict.service.SystemDataService;
import tgc.edu.exam.custom.dict.web.form.SystemDataForm;
import tgc.edu.exam.entity.Teacher;
import tgc.edu.exam.security.UserUtils;

@Controller
@RequestMapping(value="/dict/systemdata")
public class SystemDataController{

	@Autowired
	private SystemDataService sdService;
	@Autowired
	private UserUtils userUtils;

	@RequestMapping(value="/manage")
	public void manage(ModelMap map, String uuid) {
		map.put("uuid", uuid);
	}
	
	@RequestMapping(value="/edit")
	public void edit(String uuid, ModelMap map) {
		map.put("id", uuid);
	}
	
	@RequestMapping(value="/save")
	@ResponseBody
	public Object save(SystemDataForm form)  {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			SystemData model= new SystemData();
			String name = userUtils.getName();
			UUID id = form.getId();
			if(id!=null) {
				model=sdService.findById(id);
				model.setUpdateDate(sdf.format(new Date()));
				model.setUpdateName(name);
			}else {
				model.setCreateDate(sdf.format(new Date()));
				model.setCreateName(name);
			}
			BeanUtils.copyProperties(form, model,"id");
			sdService.save(model);
			return new AjaxResult("数据保存成功");
		} catch (Exception e) {
			return new AjaxResult(false,"数据保存失败");
		}
	}
	

	public Specification<SystemData> buildSpec(SystemDataForm form){
		Specification<SystemData> specification = new Specification<SystemData>() {

			private static final long serialVersionUID = 1L;
			
			@Override
			public Predicate toPredicate(Root<SystemData> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				if(form.getSystemDictionaries() != null) {
					Predicate name = cb.equal(root.get("systemDictionaries").get("id"), form.getSystemDictionaries().getId());
					rules.add(name);
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}

		};
		return specification;
	}
	
	@RequestMapping(value="/page")
	@ResponseBody
	public Object page(DataGridParam pageParam,SystemDataForm form) {
		Sort sort=Sort.by("createDate");
		Pageable pageable = pageParam.getPageable(sort);
		Specification<SystemData> spec = buildSpec(form);
		Page<SystemData> page=sdService.findAll(spec, pageable);
		return DataGridUtils.buildResult(page);
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object delete(SystemDataForm form) {
		try {
			sdService.deleteById(form.getId());
			return new AjaxResult("数据删除成功");
		} catch (Exception e) {
			return new AjaxResult(false,"数据删除失败");
		}
	}
}
